<template>
  <div>
    <tableComponents
      :isShow="false"
      @toAdd="openDio"
      :isHaveExport="false"
      :isHavePagination="false"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          header-row-class-name="head_color"
          :data="list"
          v-loading="loading"
          stripe
          tooltip-effect="dark"
          style="width: 99.9%"
        >
          <el-table-column label min-width="20">
            <template></template>
          </el-table-column>

          <el-table-column label="等级名称" min-width="50">
            <template slot-scope="scope">{{ scope.row.title }}</template>
          </el-table-column>

          <el-table-column label="累计投资金额" min-width="100">
            <template slot-scope="scope">{{ scope.row.money }}</template>
          </el-table-column>

          <el-table-column label="操作" align="center" min-width="150">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                @click="showEditDialog(scope.$index,scope.row)"
              >编辑</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                @click="deleteNew(scope.$index, scope.row.id)"
                type="danger"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>
    <el-dialog
      :title="mytitle"
      :modal-append-to-body="false"
      :visible.sync="dialogFormVisible"
      v-if="dialogFormVisible"
      class="tl size_13"
      width="800px"
    >
      <el-form :model="data" :rules="dataYZ" ref="data" label-width="120px">
        <el-form-item label="等级名称">
          <el-col :span="12">
            <el-input placeholder="请输入等级名称" v-model="data.title" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="累计投资金额">
          <el-col :span="12">
            <el-input placeholder="请输入累计投资金额" v-model="data.money" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item>
          <el-button
            :loading="$store.state.isLoading"
            size="mini"
            type="primary"
            @click="addSave('data')"
          >确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: false,
      show: false,
      myshow: false,
      mytitle: "",
      list: [],
      adminId: "",
      dialogFormVisible: false,

      uddateIndex: "",
      options: [
        {
          value: "",
          label: ""
        }
      ],
      value: "",
      data: {
        title: "",
        money: "",
        team_level: "",
        team_award: ""
      },
      dataYZ: {
        zt_num: [
          { required: true, message: "请输入直推人数", trigger: "blur" }
        ],
        team_award: [
          { required: true, message: "请输入奖励金额千分比", trigger: "blur" }
        ]
      }
    };
  },

  methods: {
    openDio() {
      this.dialogFormVisible = true;
      this.showImg = false;
      this.adminId = "";
      this.data = {
        zt_num: "",
        team_award: ""
      };
      this.mytitle = "添加团队奖";
    },
    getAdminDetails(id) {
      var data = {};
      data["id"] = id;
      this.$api.getDapp.DetailCoinTeam(data).then(res => {
        this.data = res.data.result;
      });
    },
    getNewsAll() {
      this.loading = true;
      var data = { cate_id: "" };
      this.$api.getDapp.getDappRating(data).then(res => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    handleSwitch(val, id) {
      var params = {};
      params["id"] = id;
      val == true ? (params["show"] = 1) : (params["show"] = 0);
      this.$api.getAdminuser.myChangeShow(params).then(res => {
        if (res.data.status == 0) {
          this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
        }
      });
    },
    deleteNew(index, id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var params = {};
          params.id = id;

          this.$api.getDapp.DelDappRating(params).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.list.splice(index, 1);
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        } else if (willDelete.dismiss == "cancel") {
          this.$swal("已取消", "您取消了删除操作", "error");
        }
      });
    },
    handChange(val) {
      this.data.role_id = val;
    },
    showEditDialog(index, row) {
      this.uddateIndex = index;
      this.adminId = row.id;
      this.dialogFormVisible = true;
      this.data = row;
      this.mytitle = "编辑团队奖";
    },
    addSave(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var params = {};
          params["title"] = this.data.title;
          params["money"] = this.data.money;
          params["team_level"] = this.data.team_level;
          params["team_award"] = this.data.team_award;
          if (this.adminId) {
            this.$api.getDapp.SaveEditDappRating(this.data).then(res => {
              if (res.data.code == 200 && res.data.result != null) {
                this.$fnc.alertSuccess({ text: "修改成功" }); //成功弹出框
                this.dialogFormVisible = false;
                this.list[this.uddateIndex] = this.data;
              } else {
                this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
              }
            });
          } else {
            params["title"] = this.data.title;
            params["money"] = this.data.money;
            params["team_level"] = this.data.team_level;
            params["team_award"] = this.data.team_award;
            this.$api.getDapp.SaveAddDappRating(params).then(res => {
              if (res.data.code == 200 && res.data.result != null) {
                this.$fnc.alertSuccess({ text: "添加成功" }); //成功弹出框
                this.dialogFormVisible = false;
                this.getNewsAll();
              } else {
                this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
              }
            });
          }
        } else {
          return false;
        }
      });
    }
  },

  created() {
    this.getNewsAll();
  },
  computed: {},
  mounted() {
    if (this.adminId) {
      this.getAdminDetails(this.adminId);
    }
  },
  watch: {}
};
</script>
